import {
    getBaseUrl,
    requestUtil
} from '../../utils/requestUtil.js';

import regeneratorRuntime from '../../lib/runtime/runtime';

// pages/category/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: '',
        scrollTop:0, //设置竖向滚动条位置
        currentIndex:0,   //当前选中左侧菜单的索引
        leftMenuList: [], //左侧菜单数据
        rightContext: [] //右侧商品数据
   
    },


    Cates: [],
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("onLoad");
        const baseUrl = getBaseUrl();
        this.setData({
            baseUrl
        })
        this.getCates();
    },

    //获取商品分类数据
    async getCates() {
        const result = await requestUtil({
            url: '/bigType/findCategories',
            method: "GET"
        });
        this.Cates = result.message;
        this.Cates.map((v) => {
            return v.name
        })
        let leftMenuList = this.Cates.map((v) => {
            return v.name
        })
        let rightContext = this.Cates[0].smallTypeList;
        this.setData({
            leftMenuList,
            rightContext,
           
        })

    },

    // 获取商品分类数据 从首页过来
    async getCates2(index) {
        const baseUrl = getBaseUrl();
        const result = await requestUtil({
            url: '/bigType/findCategories',
            method: "GET"
        });
        this.Cates = result.message;
        this.Cates.map((v) => {
            return v.name
        })
        let leftMenuList = this.Cates.map((v) => {
            return v.name
        })
        let rightContext = this.Cates[index].smallTypeList;
        this.setData({
            leftMenuList,
            rightContext,
            currentIndex:index,
            scrollTop:0,
            baseUrl
        })

    },

    //左侧菜单点击切换事件
    handleMenuItemChange(e){
        const {index}=e.currentTarget.dataset;
        let rightContext = this.Cates[index].smallTypeList;
        this.setData({
            currentIndex:index,
            rightContext,
            scrollTop:0,
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow:function() {
        console.log("onShow");
        const app=getApp();
        const {index}= app.globalData;
        console.log("index="+index);

        if(index!=-1){
            // let rightContext = this.Cates[index].smallTypeList;
            // this.setData({
            //     leftMenuList,
            //     rightContext
            // })
            this.getCates2(index);
            app.globalData.index=-1;
        }
    },

})